class Solution(object):
    def merge(self, intervals):
        """
        :type intervals: List[List[int]]
        :rtype: List[List[int]]
        """
        intervals = sorted(intervals) 
        i = 0
        ans = []
        while i < len(intervals):
            end = intervals[i][1]
            j = i + 1
            while j < len(intervals) and intervals[j][0] <= end:
                end = max(end, intervals[j][1])
                j += 1
            ans.append([intervals[i][0], end])
            i = j
        return ans
